package com.nut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import com.nut.common.Result;
import com.nut.entity.GoodsStock;
import com.nut.mapper.GoodsStockMapper;
import com.nut.service.GoodsStockService;

@Service
@Slf4j
public class GoodsStockServiceImpl implements GoodsStockService {

    @Autowired
    private GoodsStockMapper goodsStockMapper;

    @Override
    public List<GoodsStock> list(Integer goodsId) {
        LambdaQueryWrapper<GoodsStock> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(GoodsStock::getGoodsId, goodsId);
        List<GoodsStock> list = goodsStockMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Result<IPage<GoodsStock>> listPage(Integer pageNo, Integer pageSize) {

        Page<GoodsStock> page = new Page<>(pageNo, pageSize);
        List<GoodsStock> list = goodsStockMapper.listPage(page, 1);
        page.setRecords(list);
        return Result.ok(page);
    }

    @Override
    public Result<IPage<GoodsStock>> selectPage(Integer pageNo, Integer pageSize) {

        Page<GoodsStock> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<GoodsStock> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByDesc(GoodsStock::getId);

        IPage<GoodsStock> list = goodsStockMapper.selectPage(page, queryWrapper);
        return Result.ok(list);
    }

    @Override
    public Result<GoodsStock> getById(Serializable id) {
        GoodsStock goodsStock = goodsStockMapper.selectById(id);
        return Result.ok(goodsStock);
    }

    @Override
    public Result add(GoodsStock goodsStock) {
        goodsStockMapper.insert(goodsStock);
        return Result.ok();
    }

    @Override
    public Result updateById(GoodsStock goodsStock) {
        goodsStockMapper.updateById(goodsStock);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            goodsStockMapper.deleteById(id);
        }
        return Result.ok();
    }
}